<?php
	header("Content-type: text/html;charset=utf8");
	require_once("../db/dbclass.php");
	require_once("../model/userinfo.php");
	require_once("../utils/common.php");
	$myDb=&DbController::getInstance();
	$tweets_id=$_POST['tweet_id'];
	$user_id=$_POST['user_id'];
	$param="SELECT user_id from yj_upvote_record WHERE tweets_id='$tweets_id' AND user_id='$user_id'";
	if(mysqli_num_rows($myDb->exec_query($param)) >= 1){
		$param="DELETE FROM yj_upvote_record WHERE user_id='$user_id' AND tweets_id='$tweets_id'";
		$result=$myDb->exec_query($param);
		if($result==TRUE){
			$param="UPDATE yj_tweets SET upvote_num=upvote_num-1 WHERE tweets_id='".$tweets_id."'";
			$result=$myDb->exec_query($param);
			if($result==TRUE){
				$result='{"result":"success"}';
			}
			else{
				$result='{"result":"failure"}';
			}
		}
		else{
			$result='{"result":"failure"}';
		}
	}
	else{
		$param="INSERT INTO yj_upvote_record(tweets_id,user_id,upvote_time) VALUES('".$tweets_id."','".$user_id."','".date('y-m-d h:i:s',time())."')";
		$result=$myDb->exec_query($param);
		if($result==TRUE){
			$param="UPDATE yj_tweets SET upvote_num=upvote_num+1 WHERE tweets_id='".$tweets_id."'";
			$result=$myDb->exec_query($param);
			if($result==TRUE){
				$result='{"result":"success"}';
			}
			else{
				$result='{"result":"failure"}';
			}
		}
		else{
			$result='{"result":"failure"}';
		}
	}
	echo $result;
?>